/*
* Storage封装
*/
const STORAGE_KEY = 'mall';
export default{
    setItem(key,value,module_name){
        var storage = this.getStorage();
        if(module_name){
            storage[module_name][key] = value
            window.sessionStorage.setItem(STORAGE_KEY,JSON.stringify(storage))
        }else{
            if(value){
                storage[key] = value;
                window.sessionStorage.setItem(STORAGE_KEY,JSON.stringify(storage))
            }else{
                window.sessionStorage.setItem(STORAGE_KEY,JSON.stringify(key))
            }

        }
    },
    //获取某一个模块下面的属性user 下面的username
    getItem(key,module_name){
        if(module_name){
            let val = this.getItem(module_name)
            if(val){
                return val[key];
            }
        }
        return this.getStorage()[key]
    },
    getStorage(){
        return JSON.parse(window.sessionStorage.getItem(STORAGE_KEY) || '{}');
    },
    clear(key,module_name){
        let val =this.getStorage();
        if(module_name){
            delete val[module_name][key];
        }else{
            delete val[key]
        }
        this.setItem(val)
    }
}